import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import i18n from "../../../../i18n";

const js3300 = {
    namespaced: true,
    state: {
        // 所有权限数据
        allData: [],
        // 权限树
        treeData: [],
        loadTree: false,
        // table数据
        dataSource: [],
        // 选中节点参数
        selectTreeParam: {},

        loadTable: false,
        // 编辑弹窗表单数据
        capForm: {
            type: '',
            name: '',
            code: '',
        },
        capvisible: false,
        // 新增、编辑flag
        isFlag: null,
        selectRow: {},
        dataPagination:{
            pageSize:14,
            size:'small',
            total: 0,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        }
    },
    mutations: {
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        // 清楚table数据
        clearTable(state) {
            state.dataSource = []
        },
        // 查询权限树
        queryMenu(state) {
            let codeInTrx = {
                trxId: "sysPermission",
                actionFlg: "Q",
            };
            state.loadTree = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.allData = [...outTrx.oaryA];
                    state.treeData = [];
                    let treeData = [];
                    let list = outTrx.oaryA;
                    //   console.log(list,'list')
                    for (let i = (list.length - 1); i >= 0; i--) {
                        if (list[i] && list[i].parentId == '-1') {
                            treeData.push({
                                title: list[i].permissionName,
                                key: list[i].id,
                                value: list[i].id,
                                type: list[i].permissionType,
                                children: [],
                            });
                            list.splice(i, 1);
                        }
                    }
                    treeData.map(item => {
                        js3300.mutations.setTreeData(state, {item, list})
                    })
                    state.treeData = treeData;
                }
            }).finally(() => {
                state.loadTree = false
            });
        },
        setTreeData(state, obj) {
            if (obj.list.length) {
                for (let i = obj.list.length - 1; i >= 0; i--) {
                    if (obj.item.key == obj.list[i].parentId) {
                        obj.item.isLeaf = false;
                        obj.item.children.push({
                            title: obj.list[i].permissionName,
                            key: obj.list[i].id,
                            value: obj.list[i].id,
                            type: obj.list[i].permissionType,
                            isLeaf: true,
                            children: [],
                        });
                        obj.list.splice(i, 1);
                    }
                }
                for (let j = 0; j < obj.item.children.length; j++) {
                    let item = obj.item.children[j]
                    let list = obj.list
                    js3300.mutations.setTreeData(state, {item, list})
                }
            }
        },
        // 设置选中节点,如果为menu菜单，查询数据
        setTreeSelect(state, val) {
            if (val.type == 'menu') {
                state.selectTreeParam = val
                this.commit('js3300/queryData')
            }
        },
        // 查询table数据
        queryData(state) {
            let parentList = state.allData.filter(el => {
                if (el.parentId == state.selectTreeParam.id) {
                    return el
                }
            })
            let menuList = parentList.filter(el => {
                if (el.permissionType === 'menu') {
                    return el
                }
            })
            let codeInTrx = {
                trxId: "sysPermission",
                actionFlg: "C",
                permissionType: menuList.length ? '' : 'button',//menu/button
                parentId: state.selectTreeParam.id,
            };
            state.dataSource = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource = outTrx.oaryA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                    state.dataPagination.total = outTrx.tblCnt;
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        // 设置选中行
        customRow(state, obj) {
            state.selectRow = {
                id: obj.id,
                parentId: obj.parentId,
            }
        },
        // 新增/编辑接口
        addAndUpdate(state, val) {
            let codeInTrx = {}
            let msg = ""
            if (val === 'EDIT') {
                msg = "Update Success!"
                codeInTrx = {
                    trxId: "sysPermission",
                    actionFlg: "U",
                    saveOrUpdate: 'update',
                    iaryA: [{
                        id: state.selectRow.id,
                        parentId: state.selectRow.parentId,
                        permissionCode: state.capForm.code,
                        permissionName: state.capForm.name,
                        permissionType: state.capForm.type === '1' ? 'menu' : 'button',//menu/button
                    }]
                };
            } else if (val === 'ADD') {
                msg = "Add Success!"
                codeInTrx = {
                    trxId: "sysPermission",
                    actionFlg: "U",
                    saveOrUpdate: 'save',
                    iaryA: [{
                        parentId: state.selectRow.id,
                        permissionCode: state.capForm.code,
                        permissionName: state.capForm.name,
                        permissionType: state.capForm.type === '1' ? 'menu' : 'button',//menu/button
                    }]
                };
            }
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    js3300.mutations.queryMenu(state)
                    js3300.mutations.queryData(state)
                    js3300.mutations.cleanForm(state)
                    message.success(msg)
                }
            }).finally(() => {
            });
        },
        // 点击新增按钮
        addData(state, val) {
            state.capForm = {
                type: "1",
                name: '',
                code: '',
            },
                state.isFlag = 'ADD'
            state.capvisible = true
        },
        // 点击编辑按钮
        updateData(state, val) {
            // console.log(val,'updateData-key')
            state.capForm = {
                type: val.permissionType === "menu" ? "1" : "2",
                name: val.permissionName,
                code: val.permissionCode,
            },
                state.isFlag = 'EDIT'
            state.capvisible = true
        },
        // 删除确认，调用接口
        deleteData(state, val) {
            let codeInTrx = {
                trxId: "sysPermission",
                actionFlg: "D",
                iaryA: [{
                    id: state.selectRow.id,
                }]
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    js3300.mutations.queryMenu(state)
                    js3300.mutations.queryData(state)
                    message.success("Delete Success!")
                }
            }).finally(() => {
            });

        },
        // 新增编辑弹窗确认
        caphandleOk(state) {
            js3300.mutations.addAndUpdate(state, state.isFlag)
            state.capvisible = false
        },
        // 新增编辑弹窗取消
        caphandleCancel(state) {
            state.capvisible = false
            js3300.mutations.cleanForm(state)
        },
        //清空弹窗数据
        cleanForm(state) {
            state.capForm = {
                type: '',
                name: '',
                code: '',
            }
        }
    },
    getters: {},
    action: {}
}
export default js3300;
